package com.ruoyi.quartz.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.constant.OrderStatusEnum;
import com.ruoyi.common.core.domain.smy.MallOrder;
import com.ruoyi.system.service.smy.IMallOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component("orderTask")
public class OrderTask {

	@Resource
	private IMallOrderService mallOrderService;

	private final Logger logger = LoggerFactory.getLogger(OrderTask.class);

	/**
	 * 自动取消系统中长时间未付款的订单
	 * @Date 2022/11/8 10:50
	 * @author ZhanCheng.Li
	 */
	public void autoCancelNoPayOrder() {
		MallOrder noPayQueryDto = new MallOrder();
		noPayQueryDto.setStatus(1);
		noPayQueryDto.setPaystatus(2);
		noPayQueryDto.setMarketingtype(0);
		List<MallOrder> orderList = mallOrderService.selectMallOrderList(noPayQueryDto);
		noPayQueryDto.setStatus(Integer.valueOf(OrderStatusEnum.WAIT_PAY.getStatusId()));
		List<MallOrder> orderList2 = mallOrderService.selectMallOrderList(noPayQueryDto);
		orderList.addAll(orderList2);
		noPayQueryDto.setStatus(Integer.valueOf(OrderStatusEnum.NO_PAY_ORDER.getStatusId()));
		List<MallOrder> orderList3 = mallOrderService.selectMallOrderList(noPayQueryDto);
		orderList.addAll(orderList3);
		if(CollectionUtil.isEmpty(orderList)){
			logger.info("定时任务取消订单完成：0待处理");
		}
		for(MallOrder mallOrder : orderList){
			try {
				if(DateUtil.between(mallOrder.getCreatetime(),new Date(), DateUnit.MINUTE)>60){
					mallOrder.setStatus(Integer.valueOf(OrderStatusEnum.SHOP_ORDER_CANCEL.getStatusId()));
					mallOrder.setCanceltime(new Date());
					mallOrder.setCancelnote("未在60分钟内完成支付，系统自动取消");
					mallOrderService.updateMallOrder(mallOrder);
					logger.info("定时任务取消订单完成："+mallOrder.getId());
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("定时任务取消订单："+mallOrder.getId()+"异常："+e.getMessage());
			}
		}
	}


	/**
	 * 自动完成订单
	 * NO_PAY_ORDER("-20","免费吃订单"),
	 * SHOP_ORDER_NEW("100","新订单"),
	 * SHOP_ORDER_RECEIVE("150","已接单"),
	 * SHOP_ORDER_ARRIVED("180","已到场"),
	 * @Date 2022/11/8 11:11
	 * @author ZhanCheng.Li
	 */
	public void autoReceiptOrder() {
		List<MallOrder> orderList = new ArrayList<>();
//		MallOrder noPayQueryDto = new MallOrder();
//		noPayQueryDto.setStatus(-20);
//		noPayQueryDto.setIsaftersale(0);
//		List<MallOrder> noPayOrderList = mallOrderService.selectMallOrderList(noPayQueryDto);
//		if(CollectionUtil.isNotEmpty(noPayOrderList)){
//			orderList.addAll(noPayOrderList);
//		}
//		MallOrder newOrderQueryDto = new MallOrder();
//		newOrderQueryDto.setStatus(100);
//		newOrderQueryDto.setIsaftersale(0);
//		List<MallOrder> newOrderList = mallOrderService.selectMallOrderList(newOrderQueryDto);
//		if(CollectionUtil.isNotEmpty(newOrderList)){
//			orderList.addAll(newOrderList);
//		}
//		MallOrder shopReceiveQueryDto = new MallOrder();
//		shopReceiveQueryDto.setStatus(150);
//		shopReceiveQueryDto.setIsaftersale(0);
//		List<MallOrder> shopReceiveOrderList = mallOrderService.selectMallOrderList(shopReceiveQueryDto);
//		if(CollectionUtil.isNotEmpty(shopReceiveOrderList)){
//			orderList.addAll(shopReceiveOrderList);
//		}
//		MallOrder shopArriveQueryDto = new MallOrder();
//		shopArriveQueryDto.setStatus(180);
//		shopArriveQueryDto.setIsaftersale(0);
//		List<MallOrder> shopArriveOrderList = mallOrderService.selectMallOrderList(shopArriveQueryDto);
//		if(CollectionUtil.isNotEmpty(shopArriveOrderList)){
//			orderList.addAll(shopArriveOrderList);
//		}
		if(CollectionUtil.isEmpty(orderList)){
			logger.info("定时任务自动确认收货完成：0待处理");
		}
		for(MallOrder mallOrder : orderList){
			try {
				if(DateUtil.between(mallOrder.getDeliverytime(),new Date(),DateUnit.DAY)>7){
					mallOrder.setStatus(500);
					mallOrder.setReceipttime(new Date());
					mallOrderService.updateMallOrder(mallOrder);
					logger.info("超7天自动收货完成："+mallOrder.getId());
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("超7天自动收货："+mallOrder.getId()+"异常："+e.getMessage());
			}
		}
	}




}
